package com.shenda;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import com.swak.ui.OS;

/**
 * 无模块
 * 
 * @author lifeng
 * @date 2020年5月26日 上午11:16:53
 */
public class NoneModuler {

	/**
	 * 忽略非法反射警告 适用于jdk11
	 */
	protected static void disableAccessWarnings() {
		if (OS.java().compareTo("1.9") > 0) {
			try {
				Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
				Field field = unsafeClass.getDeclaredField("theUnsafe");
				field.setAccessible(true);
				Object unsafe = field.get(null);

				Method putObjectVolatile = unsafeClass.getDeclaredMethod("putObjectVolatile", Object.class, long.class,
						Object.class);
				Method staticFieldOffset = unsafeClass.getDeclaredMethod("staticFieldOffset", Field.class);

				Class<?> loggerClass = Class.forName("jdk.internal.module.IllegalAccessLogger");
				Field loggerField = loggerClass.getDeclaredField("logger");
				Long offset = (Long) staticFieldOffset.invoke(unsafe, loggerField);
				putObjectVolatile.invoke(unsafe, loggerClass, offset, null);
			} catch (Exception ignored) {
			}
		}
	}
}
